package com.evil.common.application.enums.robot;

import com.evil.common.application.enums.judge.JudgeTypeEnum;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 规则变更类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum RobotRuleChangeTypeEnum {

    /**
     * 规则变更类型 （1-添加数据，2-更新已有数据，3-覆盖数据）
     */
    ADD(1, "添加数据"),
    UPDATE(2, "更新已有数据"),
    COVER(3, "覆盖数据"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<RobotRuleChangeTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static RobotRuleChangeTypeEnum isExistById(Integer id) {
        return RobotRuleChangeTypeEnum.findById(id).orElseThrow(JudgeTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "规则变更类型参数错误");
    }
}
